-- HugeHole
-- Created by chengb Aug/4/2016
-- 巨坑

require "game/ui/form/dungeon/special/UIHugeHole"

-- 初始化
function initHugeHole(self)
    self.item:removeAllChildren();

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            if self.gridData.type ~= GRID_TYPE_HUGE_HOLE then
                -- 格子已经被转换
                return;
            end

            if self.transforming == true then
                trace("HugeHole", "格子转换中...");
                return;
            end

            -- 打开巨坑界面
            AudioM.playFx("ui_open1");
            local uiHugeHole = UIHugeHole.create(self.gridData);
            UIMgr.getCurrentScene():removeFormByName("UIHugeHole");
            UIMgr.getCurrentScene():addForm(uiHugeHole);
        end
    end

    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);
    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.icon:setVisible(true);
end

-- 埋葬巨坑
function buryHugeHole(self)
    local function callback()
        DungeonActionM.go("bury_huge_hole", self.gridData:getPos());
        self.transforming = false;
        alert(getLocStr("bury_huge_hole_succ"));
    end

    -- 标记格子转换中
    self.transforming = true;

    performWithDelay(self, callback, 1.5);

    -- 播放大预言术光效
    playPredictionEffect();
end
